package com.hsy.swu.utils

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayout.Tab
import com.hsy.swu.R
import com.hsy.swu.hsyutils.setFont

fun Tab.setCustomView(
    context: Context,
    title: String,
    iconResId: Int
) {
    setCustomView(
        LayoutInflater.from(context).inflate(R.layout.layout_tab_view, null).apply {
            findViewById<TextView>(R.id.title).apply {
                text = title
                setTextColor(resources.getColor(R.color.blue))
            }
            findViewById<ImageView>(R.id.icon).setImageResource(iconResId)
        }
    )
}

fun TabLayout.init() {
    addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
        @SuppressLint("ResourceType")
        override fun onTabSelected(tab: Tab?) {
            setFont()
            tab?.customView?.findViewById<TextView>(R.id.title)?.visibility = View.VISIBLE
        }

        override fun onTabUnselected(tab: Tab?) {
            tab?.customView?.findViewById<TextView>(R.id.title)?.visibility = View.GONE
        }

        override fun onTabReselected(tab: Tab?) {
        }
    })
}